package game.games.carcassonne;

import game.Logger;
import game.games.carcassonne.Tile.TYPE;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.imageio.ImageIO;

public class TileLoader
{
    private final static boolean    LOAD_LARGE_IMAGES = false;
    
    private static int              cellSize          = Integer.MIN_VALUE;
    private final static List<Tile> tiles;
    static
    {
        List<Tile> tileList = new ArrayList<Tile>();
        while (tileList.size() < 24)
        {
            String tileName = null;
            try
            {
                StringBuilder sb = new StringBuilder();
                // create filename
                sb.append("tiles/");
                if (LOAD_LARGE_IMAGES)
                {
                    sb.append("w76/");
                }
                else
                {
                    sb.append("w46/");
                }
                sb.append("Tile_");
                if (tileList.size() < 10)
                {
                    sb.append("0");
                }
                sb.append(tileList.size());
                sb.append(".bmp");
                tileName = sb.toString();
                sb.replace(0, sb.length() - 1, "");
                // load bitmap
                BufferedImage bitmap = ImageIO.read(TileLoader.class.getResourceAsStream(tileName));
                if (bitmap.getWidth() != bitmap.getHeight())
                {
                    throw new IllegalArgumentException("bitmap is not square: " + tileName);
                }
                int cellSize = bitmap.getWidth();
                if (TileLoader.cellSize == Integer.MIN_VALUE)
                {
                    TileLoader.cellSize = cellSize;
                }
                else if (TileLoader.cellSize != cellSize)
                {
                    throw new IllegalArgumentException("bitmap-size is not equal to size " + TileLoader.cellSize + ": " + cellSize);
                }
                // detect types
                TYPE type = null;
                TYPE top = null;
                TYPE left = null;
                TYPE right = null;
                TYPE bottom = null;
                boolean wappen = false;
                switch (tileList.size())
                {
                    case 0:
                    {
                        type = TYPE.WIESE;
                        top = TYPE.BURG;
                        left = TYPE.WIESE;
                        right = TYPE.WIESE;
                        bottom = TYPE.WIESE;
                        wappen = false;
                        break;
                    }
                    case 1:
                    {
                        type = TYPE.WIESE;
                        top = TYPE.BURG;
                        left = TYPE.WIESE;
                        right = TYPE.BURG;
                        bottom = TYPE.WIESE;
                        wappen = false;
                        break;
                    }
                    case 2:
                    {
                        type = TYPE.WIESE;
                        top = TYPE.WIESE;
                        left = TYPE.BURG;
                        right = TYPE.BURG;
                        bottom = TYPE.WIESE;
                        wappen = false;
                        break;
                    }
                    case 3:
                    {
                        type = TYPE.STRASSE;
                        top = TYPE.BURG;
                        left = TYPE.WIESE;
                        right = TYPE.STRASSE;
                        bottom = TYPE.STRASSE;
                        wappen = false;
                        break;
                    }
                    case 4:
                    {
                        type = TYPE.STRASSE;
                        top = TYPE.BURG;
                        left = TYPE.STRASSE;
                        right = TYPE.WIESE;
                        bottom = TYPE.STRASSE;
                        wappen = false;
                        break;
                    }
                    case 5:
                    {
                        type = TYPE.KREUZUNG;
                        top = TYPE.BURG;
                        left = TYPE.STRASSE;
                        right = TYPE.STRASSE;
                        bottom = TYPE.STRASSE;
                        wappen = false;
                        break;
                    }
                    case 6:
                    {
                        type = TYPE.STRASSE;
                        top = TYPE.BURG;
                        left = TYPE.STRASSE;
                        right = TYPE.STRASSE;
                        bottom = TYPE.WIESE;
                        wappen = false;
                        break;
                    }
                    case 7:
                    {
                        type = TYPE.WIESE;
                        top = TYPE.BURG;
                        left = TYPE.BURG;
                        right = TYPE.WIESE;
                        bottom = TYPE.WIESE;
                        wappen = false;
                        break;
                    }
                    case 8:
                    {
                        type = TYPE.STRASSE;
                        top = TYPE.BURG;
                        left = TYPE.BURG;
                        right = TYPE.STRASSE;
                        bottom = TYPE.STRASSE;
                        wappen = false;
                        break;
                    }
                    case 9:
                    {
                        type = TYPE.WIESE;
                        top = TYPE.BURG;
                        left = TYPE.BURG;
                        right = TYPE.WIESE;
                        bottom = TYPE.WIESE;
                        wappen = true;
                        break;
                    }
                    case 10:
                    {
                        type = TYPE.STRASSE;
                        top = TYPE.BURG;
                        left = TYPE.BURG;
                        right = TYPE.STRASSE;
                        bottom = TYPE.STRASSE;
                        wappen = true;
                        break;
                    }
                    case 11:
                    {
                        type = TYPE.BURG;
                        top = TYPE.WIESE;
                        left = TYPE.BURG;
                        right = TYPE.BURG;
                        bottom = TYPE.WIESE;
                        wappen = false;
                        break;
                    }
                    case 12:
                    {
                        type = TYPE.BURG;
                        top = TYPE.WIESE;
                        left = TYPE.BURG;
                        right = TYPE.BURG;
                        bottom = TYPE.WIESE;
                        wappen = true;
                        break;
                    }
                    case 13:
                    {
                        type = TYPE.BURG;
                        top = TYPE.BURG;
                        left = TYPE.BURG;
                        right = TYPE.BURG;
                        bottom = TYPE.WIESE;
                        wappen = false;
                        break;
                    }
                    case 14:
                    {
                        type = TYPE.BURG;
                        top = TYPE.BURG;
                        left = TYPE.BURG;
                        right = TYPE.BURG;
                        bottom = TYPE.STRASSE;
                        wappen = false;
                        break;
                    }
                    case 15:
                    {
                        type = TYPE.BURG;
                        top = TYPE.BURG;
                        left = TYPE.BURG;
                        right = TYPE.BURG;
                        bottom = TYPE.WIESE;
                        wappen = true;
                        break;
                    }
                    case 16:
                    {
                        type = TYPE.BURG;
                        top = TYPE.BURG;
                        left = TYPE.BURG;
                        right = TYPE.BURG;
                        bottom = TYPE.STRASSE;
                        wappen = true;
                        break;
                    }
                    case 17:
                    {
                        type = TYPE.BURG;
                        top = TYPE.BURG;
                        left = TYPE.BURG;
                        right = TYPE.BURG;
                        bottom = TYPE.BURG;
                        wappen = true;
                        break;
                    }
                    case 18:
                    {
                        type = TYPE.KLOSTER;
                        top = TYPE.WIESE;
                        left = TYPE.WIESE;
                        right = TYPE.WIESE;
                        bottom = TYPE.WIESE;
                        wappen = false;
                        break;
                    }
                    case 19:
                    {
                        type = TYPE.KLOSTER;
                        top = TYPE.WIESE;
                        left = TYPE.WIESE;
                        right = TYPE.WIESE;
                        bottom = TYPE.STRASSE;
                        wappen = false;
                        break;
                    }
                    case 20:
                    {
                        type = TYPE.STRASSE;
                        top = TYPE.STRASSE;
                        left = TYPE.WIESE;
                        right = TYPE.WIESE;
                        bottom = TYPE.STRASSE;
                        wappen = false;
                        break;
                    }
                    case 21:
                    {
                        type = TYPE.STRASSE;
                        top = TYPE.WIESE;
                        left = TYPE.STRASSE;
                        right = TYPE.WIESE;
                        bottom = TYPE.STRASSE;
                        wappen = false;
                        break;
                    }
                    case 22:
                    {
                        type = TYPE.KREUZUNG;
                        top = TYPE.WIESE;
                        left = TYPE.STRASSE;
                        right = TYPE.STRASSE;
                        bottom = TYPE.STRASSE;
                        wappen = false;
                        break;
                    }
                    case 23:
                    {
                        type = TYPE.KREUZUNG;
                        top = TYPE.STRASSE;
                        left = TYPE.STRASSE;
                        right = TYPE.STRASSE;
                        bottom = TYPE.STRASSE;
                        wappen = false;
                        break;
                    }
                }
                // Init Tile
                Tile tile = new Tile(bitmap, type, top, left, right, bottom, wappen);
                tileList.add(tile);
            }
            catch (IOException ex)
            {
                Logger.log("Failed to load bitmap: " + tileName, ex);
            }
        }
        tiles = Collections.unmodifiableList(tileList);
    }
    
    public static AngledTile getTile(int index)
    {
        Tile tile = tiles.get(index);
        return new AngledTile(tile, 0);
    }
    
    public static int sumTiles()
    {
        return tiles.size();
    }
}
